﻿namespace EFS.Common.UI.Resources.ImageSource
{
	public static class Cache
	{
		// cached sources
		private static System.Collections.Generic.Dictionary<System.String, System.Windows.Media.ImageSource> cachedImageSources =
			new System.Collections.Generic.Dictionary<System.String, System.Windows.Media.ImageSource>();

		/// Get surce
		public static System.Windows.Media.ImageSource GetSource(System.String uniqueName)
		{
			// init
			System.Windows.Media.ImageSource imageSource = null;

			// check
			if (cachedImageSources.ContainsKey(uniqueName))
			{
				imageSource = cachedImageSources[uniqueName];
			}

			return imageSource;
		}

		/// <summary>
		/// Contains
		/// </summary>
		/// <param name="uniqueName"></param>
		/// <returns></returns>
		public static System.Boolean ContainsSource(System.String uniqueName)
		{
			// check
			return cachedImageSources.ContainsKey(uniqueName);
		}

		/// <summary>
		/// Add
		/// </summary>
		/// <param name="bitmap"></param>
		public static void Add(System.Drawing.Bitmap bitmap, System.String uniqueName)
		{
			// check
			if (!cachedImageSources.ContainsKey(uniqueName))
			{
				// cachle
				cachedImageSources[uniqueName] = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
						bitmap.GetHbitmap(),
						System.IntPtr.Zero,
						System.Windows.Int32Rect.Empty,
						System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
			}
		}

		/// ToImageSource
		public static void Add(System.Drawing.Icon icon, System.String uniqueName)
		{
			// check
			if (!cachedImageSources.ContainsKey(uniqueName))
			{
				// create 
				cachedImageSources[uniqueName] = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
					icon.ToBitmap().GetHbitmap(),
					System.IntPtr.Zero,
					System.Windows.Int32Rect.Empty,
					System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
			}
		}
	}
}
